# -*- coding: utf-8 -*-

from flask import request

from autoscrapy.server import base
from autoscrapy.utils import log


LOG = log.get_logging(__name__)


class crawlLink(base.controllerBase):

    PRE_URL = '/links'
    DEFAULT_METHODS = ['GET', 'POST']
    COLLECTION = 'crawllinks'
    TYPE = 'type'
    SEED = 'seed'

    def _process_request(self, db, **kw):
        # add links
        if request.method == 'POST':
            linkjosn = request.get_json(silent=True)
            links = linkjosn['links']
            for _link in links:
                link = {
                    self.TYPE: _link[self.TYPE],
                    self.SEED: _link[self.SEED]
                }
                db[self.COLLECTION].insert_one(link)
            return {'res': 'OK'}
        else:
            # show all will crawl links
            links = db[self.COLLECTION].find()
            res = {'links': []}
            limit = (int)(request.args.get('limit', 0))
            _count = 0
            for _link in links:
                _count += 1
                _re = {
                    self.TYPE: _link[self.TYPE],
                    self.SEED: _link[self.SEED]
                }
                res['links'].append(_re)
                if limit and (_count >= limit):
                    break

            return res

